package com.motu.monstercity.server.login.notice;

import com.motu.monstercity.module.common.utility.Notice;
import com.motu.monstercity.protocol.CommonHttp.PBNotice;
import com.motu.monstercity.protocol.CsHttp.CSGetNoticeResponse;
import com.motu.vertx.module.utility.commondata.WordManager;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class NoticeManager {

    private static final Logger logger = LoggerFactory.getLogger(NoticeManager.class);

    private static ConcurrentHashMap<Long, Notice> noticeMap = new ConcurrentHashMap<Long, Notice>();

    public static void init(List<Future> futures) {
        futures.add(initNotice());
    }
    /**
     * 初始化公告
     */
    public static Future<Void> initNotice() {
        Promise promise = Promise.promise();
        String sql = "select * from notice;";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getServerPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                ConcurrentHashMap<Long, Notice> tmp = new ConcurrentHashMap<Long, Notice>();
                JsonArray list = res.result();
                logger.info("init notice size:" + list.size());
                for (int i = 0; i < list.size(); i++) {
                    Notice entity = new Notice(list.getJsonObject(i));
                    tmp.put(entity.getId(), entity);
                }
                noticeMap.clear();
                noticeMap = tmp;
                promise.complete();
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    /**
     * 获取公告
     */
    public static void getNotice(CSGetNoticeResponse.Builder res_build, int serverId, int platform, List<PBNotice> noticeList, int language) {
        for (Notice notice : noticeMap.values()) {
            if (notice.getType() != Notice.NOTICE_TYPE_ALL) { // 如果不是全部显示的公告，再判断一下
                if (serverId == 0 && notice.getType() != Notice.NOTICE_TYPE_HOME_PAGE) {
                    continue;
                }
                if (serverId != 0 && notice.getType() != Notice.NOTICE_TYPE_GAME) {
                    continue;
                }
            }
            if (notice.isStart(platform, serverId, language)) {
                res_build.addIdList(notice.getId());
                if (isNewNotice(notice, noticeList)) {
                    res_build.addNoticeList(takeBuilder(notice, language));
                }
            }
        }
        res_build.setServerId(serverId);
    }

    private static boolean isNewNotice(Notice notice, List<PBNotice> noticeList) {
        for (PBNotice pbn : noticeList) {
            if (notice.getId() == pbn.getId() && notice.getHashCode() == pbn.getHashcode()) {
                return false;
            }
        }
        return true;
    }

    private static PBNotice.Builder takeBuilder(Notice notice, int language) {
        PBNotice.Builder sb = PBNotice.newBuilder();
        sb.setId(notice.getId());
        sb.setType(notice.getType());
        sb.setTitle(Notice.getWordByLanguage(notice.getTitleList(), language));
        sb.setContent1(WordManager.getWordByLanguage(notice.getContent1(), language));
        sb.setContent2(Notice.getWordByLanguage(notice.getContentList(), language));
        sb.setStartTime(notice.getStartTime());
        sb.setEndTime(notice.getEndTime());
        sb.setSort(notice.getSort());
        sb.setStatus(notice.getStatus());
        sb.setHashcode(notice.getHashCode());
        sb.setIcon(notice.getIcon());
        sb.setUrl(notice.getUrl());
        sb.setUrlIcon(notice.getUrlIcon());
        sb.setIsConstrain(notice.getIsConstrain() == (byte) 1);
        return sb;
    }

    public static PBNotice.Builder getMaintainNotice(int serverId, int platform, int language) {
        for (Notice notice : noticeMap.values()) {
            if (notice.getType() == Notice.NOTICE_TYPE_MAINTAIN) {// 取维护公告
                if (notice.isStart(platform, serverId, language)) {
                    return takeBuilder(notice, language);
                }
            }
        }
        return null;
    }

}
